<template>
  <div class="login">
    <div class="login-box">
      <div class="box-img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        ref="loginformref"
        :rules="loginrules"
        :model="loginform"
        label-width="0px"
        class="form_box"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginform.username"
            prefix-icon="iconfont icon-gerenyonghutouxiang2"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginform.password"
            prefix-icon="iconfont icon-lock-full"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>

          <el-button type="info" @click="loginreset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginform: {
        username: 'admin',
        password: '123456',
      },
      loginrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    loginreset() {
      // console.log(this)
      this.$refs.loginformref.resetFields()
    },
    login() {
      this.$refs.loginformref.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return
        if (
          this.loginform.username == 'admin' &&
          this.loginform.password == 123456
        ) {
          // console.log(this.loginform.username,this.loginform.password)
          this.$message.success('登录成功')
        } else {
          this.$message.error('用户名或者密码不正确')
        }
        window.sessionStorage.setItem('token', 'fan123456liang')
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login {
  background: #2b4b6b;
  height: 100%;
  .login-box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .box-img {
      width: 140px;
      height: 140px;
      padding: 10px;
      border: 1px solid #bed8f3e0;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #d9dadb;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eff5f5;
      }
    }
  }
}
.form_box {
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>